<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="form" :model="queryParams" :inline="true">
        <el-form-item prop="phone" label="用户手机号">
          <el-input
            v-model="queryParams.phone"
            placeholder="用户手机号"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery" icon="el-icon-search">搜索</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <template #header>
        <el-button
          type="warning"
          @click="syncFmtAnswers()"
          ><i class="el-icon-refresh"></i> 同步问卷</el-button>
        <el-button
          type="success"
          @click="exportExcel()"
          ><i class="el-icon-document"></i> 导出Excel</el-button>
      </template>

      <el-table
        v-loading="loading"
        :data="dataList"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" width="55" prop="dataIndex"/>
        <el-table-column label="问卷回答者" prop="userName"/>
        <el-table-column label="用户手机号" prop="phone"/>
        <el-table-column label="所填问卷" prop="surveyName"/>
        <el-table-column label="是否合格" prop="qualifiedText"/>
        <el-table-column label="填写时间" prop="createTimeText"/>
        <el-table-column fixed="right" label="操作"  min-width="280" width="280">
          <template #default="scope">
            <el-button type="warning" size="small" link @click="openAnswerDialog(scope.row)"><i class="el-icon-view"></i> 查看回答</el-button>
            <el-button type="success" :disabled="scope.row.qualified != null && scope.row.qualified" size="small" link @click="setFmtDonotQualified(scope.row)"><i class="el-icon-view"></i> 供者合格</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.pageNum"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>

    <el-dialog
      :visible.sync="dialog.visible"
      :title="dialog.title"
      width="800px"
      class="answerDetail"
      :before-close="closeDialog"
      :close-on-click-modal="false"
    >
      <div v-for="(answer, index) in answerDtos" :key="answer.question">
        <div>
          <span>{{index + 1}}、</span>
          <span>{{answer.question}}</span>
          <span style="margin-left: 10px; font-weight: 600;">{{answer.answer}}</span>
        </div>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import fmtAnswerApi from '@/api/aliyun/questionnaire/fmtAnswer'
import fmtDonorApi from '@/api/aliyun/fmt/donor'
import { formatDate } from '@/utils/formatTime'
export default {
	name: 'userIndex',
	data() {
		return {
			queryParams: {
        phone: '',
        pageNum: 0,
        pageSize: 10,

      },
      total: 0,
      loading: false,
      dataList: [],
      ids: [],
      dialog: {},
      formData: {},
      rules: {},

      checkedData: {},
      answerDtos: [],
		};
	},
  methods: {
    setFmtDonotQualified (row) {
      this.$confirm('是否要将当前问卷所属用户标记为合格供者?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        fmtDonorApi.donorQualified(row.id).then(res => {
          this.$message({message: 'FMT 供者标记合格成功！', type: 'success'});
          this.handleQuery();
        })
      }).catch(() => {});
      
    },
    exportExcel () {
      fmtAnswerApi.exportAnswers().then(res => {
        const url = window.URL.createObjectURL(res)
        const link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download', '问卷答案.xlsx')
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
        URL.revokeObjectURL(url)
      })
    },
    syncFmtAnswers () {
      fmtAnswerApi.syncFmtAnswers().then(res => {
        this.$message({message: 'FMT 问卷答案同步成功！', type: 'success'});
        this.handleQuery();
      })
    },
    closeDialog () {
      this.$set(this.dialog, 'visible', false);
    },
    openAnswerDialog (row) {
      this.$set(this.dialog, 'visible', true);
      this.$set(this.dialog, 'title', '查看回答');
      fmtAnswerApi.detail(row.id).then(res => {
        this.answerDtos = res.data.answerDtos;
      })
      
    },
    handleQuery() {
      this.loading = true;
      let params = {
        number: this.queryParams.pageNum,
        size: this.queryParams.pageSize,
        criteriaVos: [{
          name: 'phone',
          searchType: 'LIKE',
          searchVal: this.queryParams.phone
        }]
      };
      fmtAnswerApi.page(params).then((res) => {
        let data = res.data;
        let index = 1
        data.content.forEach((v) => {
          v.createTimeText = formatDate(new Date(v.submitTime), "YYYY-mm-dd HH:MM:SS")
          v.surveyName = '供体招募健康评估量表';
          v.dataIndex = index++;
          v.qualifiedText = v.qualified == null ? '-' : (v.qualified ? '合格' : '-')
        });
        this.dataList = data.content;
        
        this.total = data.count;
        this.loading = false;
      }).catch(() => {});
    },
    resetQuery() {
      this.queryParams = {
        phone: '',
        pageNum: 0,
        pageSize: 10,
      };
      this.handleQuery();
    },
    // handleDelete(id) {
    //   if (id) {
    //     answerApi.delete(id).then((res) => {
    //       this.$message({message: '问卷答案删除成功！', type: 'success'});
    //       this.handleQuery();
    //     });
    //   } else {
    //     this.$message.error('暂不支持批量删除问卷答案！');
    //   }
    // },
    handleSelectionChange(val) {
      this.ids = [];
      val.forEach(v => {
        this.ids.push(v.id);
      });
    },
    // handleSelectionRoleChange(rows) {
    //   if (rows) {
    //     let arr = [];
    //     rows.forEach((v) => {
    //       arr.push(v.id);
    //     });
    //     this.selectRoleIdArr = arr;
    //   }
    // },
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.handleQuery();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.handleQuery();
    },
  },
  created() {
    this.handleQuery();
  }
};
</script>

<style scoped>

.answerDetail /deep/ .el-dialog__body {
    height: 80vh;
    overflow: auto;
}

.block {
  margin-top: 10px;
  float: right;
  margin-bottom: 10px;
}
</style>